﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.Test            Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: TaskProcessorPluginManagerTest.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;
using System.Collections;
using Hyldahl.Services.TaskProcessing.DisplayMessage;
using Hyldahl.Services.TaskProcessing.FibonacciCalculation;
using NUnit.Framework;
using System.Collections.Generic;

namespace Hyldahl.Services.TaskProcessing.Test
{
    
    /// <summary>
    ///This is a test class for TaskProcessorPluginManagerTest and is intended
    ///to contain all TaskProcessorPluginManagerTest Unit Tests
    ///</summary>
    [TestFixture()]
    public class TaskProcessorPluginManagerTest
    {
        /// <summary>
        ///A test for TaskProcessorPluginManager Constructor
        ///</summary>
        [Test()]
        public void TaskProcessorPluginManagerConstructorTest()
        {
            TaskProcessorPluginManager target = new TaskProcessorPluginManager(TaskProcessingApplicationContext.Current);
        }

        /// <summary>
        ///A test for TaskProcessorPluginManager Constructor
        ///</summary>
        [Test()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TaskProcessorPluginManagerConstructorNullArgumentTest()
        {
            TaskProcessorPluginManager target = new TaskProcessorPluginManager(null);
        }

        /// <summary>
        ///A test for GetTaskProcessor
        ///</summary>
        [Test()]
        public void GetTaskProcessorTest()
        {
            TaskProcessorPluginManager target = new TaskProcessorPluginManager(TaskProcessingApplicationContext.Current);

            string taskType = "Fibonacci";
            Type expected = typeof(FibonacciCalculationTaskProcessor);
            
            TaskProcessor actual = target.GetTaskProcessor(taskType);

            Assert.IsNotNull(actual);
            Assert.AreEqual(expected, actual.GetType());
        }

        /// <summary>
        ///A test for GetTaskProcessor
        ///</summary>
        [Test()]
        public void GetTaskProcessorUnknownProcessorTest()
        {
            TaskProcessorPluginManager target = new TaskProcessorPluginManager(TaskProcessingApplicationContext.Current);

            string taskType = "Unknown Processor";

            TaskProcessor actual = target.GetTaskProcessor(taskType);

            Assert.IsNull(actual);
        }

        /// <summary>
        ///A test for TaskProcessors
        ///</summary>
        [Test()]
        public void TaskProcessorsTest()
        {
            TaskProcessorPluginManager target = new TaskProcessorPluginManager(TaskProcessingApplicationContext.Current);

            List<TaskProcessor> actual = target.TaskProcessors;

            Assert.IsNotNull(actual);

            Assert.AreEqual(2, actual.Count);

            Assert.That(ListContainsInstanceOfType(typeof(FibonacciCalculationTaskProcessor), actual));
            Assert.That(ListContainsInstanceOfType(typeof(DisplayMessageTaskProcessor), actual));
        }

        /// <summary>
        /// Determines if the specified list contains an instance of the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="collection">The collection.</param>
        /// <returns></returns>
        private static bool ListContainsInstanceOfType(Type type, ICollection collection)
        {
            foreach(object o in collection)
            {
                if (o.GetType() == type)
                    return true;
            }

            return false;
        }
    }
}
